package rbac.models;

import com.et.ar.ActiveRecordBase;
import com.et.ar.annotations.BelongsTo;
import com.et.ar.annotations.Column;
import com.et.ar.annotations.Id;
import com.et.ar.annotations.Table;
import com.et.ar.annotations.Unique;

import java.sql.*;


@Table(name="oplogs")
public class Oplog extends ActiveRecordBase{
	@Id private Integer id;
	@Column private Integer loglevel;
	@Column private String logfrom;
	@Column private Timestamp eddate;
	@Column private String msg;
	@Column private String debugmsg;


	//	@BelongsTo(foreignKey="demoId")
	//	private Demo demo;

	//	public Demo getDemo() {
	//		return demo;
	//	}
	//	public void setDemo(Demo demo) {
	//		this.demo = demo;
	//	}

	//	public int getChildCount() throws Exception {
	//		return (int)count(Oplog.class, "parentId=?", new Object[]{id});
	//	}
	//	public String findChildIds() throws Exception {
	//		String s = "";
	//		List<Integer> ids = new ArrayList<Integer>();
	//		ids.add(id);
	//		while(!ids.isEmpty()){
	//			int id = ids.remove(0);
	//			s += id + ",";
	//			for(Oplog item: Oplog.findAll(Oplog.class, "parentId=?", new Object[]{id})){
	//				ids.add(item.getId());
	//			}
	//		}
	//		if (!s.equals("")){
	//			s = s.substring(0, s.length() - 1);
	//		}
	//		return s;
	//	}
	public void setId(Integer id){
		this.id=id;
	}
	public Integer getId(){
		return id;
	}

	public void setLoglevel(Integer loglevel){
		this.loglevel=loglevel;
	}
	public Integer getLoglevel(){
		return loglevel;
	}

	public void setLogfrom(String logfrom){
		this.logfrom=logfrom;
	}
	public String getLogfrom(){
		return logfrom;
	}

	public void setEddate(Timestamp eddate){
		this.eddate=eddate;
	}
	public Timestamp getEddate(){
		return eddate;
	}

	public void setMsg(String msg){
		this.msg=msg;
	}
	public String getMsg(){
		return msg;
	}

	public void setDebugmsg(String debugmsg){
		this.debugmsg=debugmsg;
	}
	public String getDebugmsg(){
		return debugmsg;
	}

	public static void addMsg(String logfrom,String msg) throws Exception {
    	addMsg(1,logfrom,msg,"");
	}
	
    public static void addMsg(String logfrom,String msg,String debugmsg) throws Exception {
    	addMsg(1,logfrom,msg,debugmsg);
	}
	
	public static void addMsg(int loglevel,String logfrom,String msg,String debugmsg) throws Exception {
		Oplog log = new Oplog();
		log.setLoglevel(loglevel);
		log.setLogfrom(logfrom);
		log.setMsg(msg);
		log.setDebugmsg(debugmsg);
		log.setEddate(new Timestamp(System.currentTimeMillis()));
		log.save();
	}
}

